package com.zjs.serice.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjs.dao.UserDao;
import com.zjs.pojo.User;
import com.zjs.serice.UserService;
import com.zjs.util.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import vo.AddUserVo;
import vo.UpdatePassword;
import vo.UserInfoVo;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceimpl<HttpCookiec> implements UserService {
    @Autowired
    private UserDao userdao;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;


    @Override
    public ResultStatus login(User user) {
        String uname = user.getUname();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUname,uname);
        wrapper.last("limit 1");
        User user1 = userdao.selectOne(wrapper);
        if (user1==null){
            return ResultStatus.FTLED(300,"用户名不存在");
        }
        String upassword = user1.getUpassword();
        if (!upassword.equals(user.getUpassword())){
            return ResultStatus.FTLED(300,"密码不存在");
        }
        Boolean aBoolean = redisTemplate.hasKey(user1.getUid() + "");
        if (aBoolean){
            redisTemplate.delete(user1.getUid() + "");
        }
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        HashMap<String, Object> payload = new HashMap<>();
        payload.put("id",user1.getUid());
        payload.put("uname",user1.getUname());
        String token = JWTUtil.createToken(payload, "123456".getBytes());

        String key = "pc:"+user1.getUid();
        value.set(key,token,30, TimeUnit.MINUTES);
        value.set(token,token,30, TimeUnit.MINUTES);

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user1,userInfoVo);
        userInfoVo.setToken(token);

        return  ResultStatus.SUCCESS(userInfoVo);
    }

    @Override
    public ResultStatus gettoken(User user) {
        Boolean aBoolean = redisTemplate.hasKey(user.getUid() + "");
        if (aBoolean){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",user.getUid());
            map.put("uname",user.getUname());
            String token = JWTUtil.createToken(map, "123456".getBytes());
            ValueOperations<String, String> value = redisTemplate.opsForValue();
            value.set(user.getUid()+"",token,30, TimeUnit.SECONDS);
            return ResultStatus.SUCCESS(token);
        }else{
            return ResultStatus.FTLED(404,"登录过期,请重新登录");
        }
    }

    @Override
    public ResultStatus updetepassword(UpdatePassword updatepassword) {
        User user = userdao.selectById(updatepassword.getUserId());
        if(user == null) {
            return ResultStatus.FTLED(404, "用户不存在");
        }
        if(!user.getUpassword().equals(updatepassword.getOldPass())) {
            return ResultStatus.FTLED(403, "密码不正确");
        }
        user.setUpassword(updatepassword.getNewPass());
        userdao.updateById(user);

        return ResultStatus.SUCCESS();
    }

    @Override
    public ResultStatus sendCode(User user) {
        String code = RandomUtil.randomNumbers(4);
        redisTemplate.opsForValue().set(user.getUtel(), code, 1, TimeUnit.MINUTES);
        log.info("给"+user.getUtel()+"发送发送短信："+code);
        return ResultStatus.SUCCESS(code);
    }

    @Override
    public ResultStatus addUser(AddUserVo addUserVo) {

        // 判断验证码
        String key = addUserVo.getMobile();
        String redisCode = (String) redisTemplate.opsForValue().get(key);
        if (redisCode == null) {
            return ResultStatus.FTLED(410, "验证码过期");
        }
        if (!redisCode.equals(addUserVo.getVerifyCode())) {
            return ResultStatus.FTLED(410, "验证码错误");
        }

        // 判断用户名是否存在(判断手机号)
        LambdaQueryWrapper<User> query = new QueryWrapper<User>().lambda()
                .eq(User::getUname, addUserVo.getUserName());
        query.or().eq(User::getUtel, addUserVo.getMobile());
        query.last("limit 1");

        User user = userdao.selectOne(query);
        if (user != null) {
            return ResultStatus.FTLED(410, "用户已存在");
        }
        // 开始注册
        User user1 = new User();
        user1.setUname(addUserVo.getUserName());
        user1.setUpassword(addUserVo.getUserPass());
        user1.setUtel(addUserVo.getMobile());


        userdao.insert(user1);

        return ResultStatus.SUCCESS();
    }


}
